                                require 'db_help'
require 'taobao_api'
class GetData
      def set_cat_info(item,cat,parent)
        while p = parent.fetch_hash do
          item[cat+'_id'] = p['t_cid']
          item[cat+'_name'] = p['cat_name']
        end
      end
      def replace(compare1,compare2, r)
        if !compare2.nil?
          index =  compare2.split(",").index(compare1)
          if !index.nil?
            r.split(",")[index]
          else
            compare1
          end
        else
          compare1
        end
      end
      def hash_to_str (h)
        val = ''
        h.each do |p|
          val += p.join(":")+";"
        end
        val.slice(0,val.length-1)
      end
      def transfer_prop_to_str_v2(dao,p)
          str={}
          cid = p['cat1_id']
          cid = p['cat2_id'] unless p['cat2_id'].nil?
          prop = p['props'].split(";")
          prop.each do |s|
            values = s.split(":")
            rs = dao.query("select property_name,name from tao_property_values where pid=#{values[0]} and vid=#{values[1]} and cid=#{cid}")
            if rs.num_rows>0
              while rst = rs.fetch_hash do
                s = replace(rst['property_name'],p['input_pids'],p['input_str'])
                if str[s].nil?
                  str[s]=rst['name']
                else
                  str[s]=str[s]+","+rst['name']
                end
              end
            end
          end
          return hash_to_str(str)
      end
      def escape(str)
          return nil if str.nil?
          str = str.gsub!(/"/,"'") unless str.gsub!(/"/,"'").nil?
          return str
      end

      def get_item_category(dao,cid,cat_level)
        begin
        dao.query('truncate item_categories;') if cid=='16'
        if cat_level <=2
          categories = TaobaoAPI.get_with(:method =>'taobao.itemcats.get',
                                      :fields=>'cid,parent_cid,name,is_parent,status,sort_order',
                                      :parent_cid=>cid)
          if categories['error_response']
              puts categories['error_response']
          else
              item_cats = categories['itemcats_get_response']['item_cats']['item_cat']

              item_cats.each do |it|
                #puts "Category name: #{it['name']}, Is parent : #{it['is_parent']}."
                #create record
                it['t_cid']=it['cid']
                it['cat_name'] = it['name']
                it['parent_id']=it['parent_cid']
                it['status']='1'
                it['cat_level']=cat_level
                if it['is_parent']=='true'||it['it_parent']==1
                  it['is_parent']=1
                else
                  it['is_parent']=0
                end
                it.delete('cid')
                it.delete('name')
                it.delete('parent_cid')
                it['table']='tao_categories'
                it['view_name']=it['cat_name']
                it['created_at']=TaobaoAPI.timestamp
                it['updated_at']=TaobaoAPI.timestamp
                dao.query(dao.build_sql(it))
                get_item_prop_values(dao,it['t_cid']) if it['is_parent'] ==0
                sleep 0.3
                get_item_category(dao,it['t_cid'],cat_level+1) if it['is_parent'] == 1
                sleep 0.3
              end#item_cats.each end
          end#if end
        end
          rescue Exception=>e
            raise e
          end
      end#init_data end
      #获取属性值
      def get_item_prop_values(dao,cid)
          begin
          #loop category
            props = TaobaoAPI.get_with(:method=>'taobao.itemprops.get',
                             :fields=>'pid,name,parent_pid,parent_vid,prop_values,sort_order',
                             :cid=>cid)
            sleep 0.3
            propvalues = TaobaoAPI.get_with(:method=>'taobao.itempropvalues.get',
                                            :fields=>'cid,pid,prop_name,vid,name,name_alias,sort_order',
                                            :cid=>cid)
           if props['error_response']
               puts props['error_response']
           else
              prop = props['itemprops_get_response']['item_props']['item_prop']
              unless prop.nil?
                #loop property
                    ah = {}
                    pvalue = 
                    prop.each do |p|
                      p['table']='tao_item_properties'
                      p['status']=0
                      p['created_at']=TaobaoAPI.timestamp
                      p['updated_at']=TaobaoAPI.timestamp
                      p.delete('prop_values')
                      dao.query(dao.build_sql(p))
                    end#p.each
              end
           end#else
           if propvalues['error_response']
             puts propvalues['error_response']
           else
             val = propvalues['itempropvalues_get_response']['prop_values'] unless  propvalues['itempropvalues_get_response'].nil?
             puts val
             unless val.nil?
                #loop prop values
                val.each do |pv|
                  puts pv
                  if pv.class.to_s=='Hash'
                    pv['status']=0
                    pv['created_at']=TaobaoAPI.timestamp
                    pv['updated_at']=TaobaoAPI.timestamp
                    dao.query(dao.build_sql(pv))
                  else
                    ah[pv[0].to_s] = pv[1].to_s
                  end                      
                end#pv.each
                if ah.keys.length>0
                  ah['table'] ='tao_property_values'
                  ah['created_at']=TaobaoAPI.timestamp
                  ah['updated_at']=TaobaoAPI.timestamp
                  ah['status']=0
                  #dao.query(dao.build_sql(ah)) 
                end
              end#unless.nil
             
           end
        rescue Exception=>e
          raise e
        end
      end#get_item_prop_values
end#class



dao = DBHelp.new
o = GetData.new
o.get_item_category(dao,16,0)